{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "24cd6f90-7395-4812-b375-cc2f984e2a85",
   "metadata": {},
   "source": [
    "Chapter 08\n",
    "# 全局变量和局部变量的传递路径\n",
    "Book_1《编程不难》 | 鸢尾花书：从加减乘除到机器学习 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "15722d74-039e-4a1f-9ef2-c44e863d0f4a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "global_x: 8\n"
     ]
    }
   ],
   "source": [
    "# 全局变量\n",
    "global_x = 8\n",
    "print(\"global_x:\", global_x) # global_x: 8"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "fadad4ea-73e6-494b-b383-682000c8f84c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 自定义函数\n",
    "def my_function(local_x):\n",
    "    # local_x 局部变量\n",
    "    print(\"local_x:\", local_x) # local_x: 38\n",
    "    \n",
    "    # 创建一个同名的局部变量\n",
    "    global_x = 88  # global_x: 88\n",
    "    print(\"global_x:\", global_x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "7871cf7d-cda2-48f7-835a-2f0178a5d953",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "local_x: 38\n",
      "global_x: 88\n"
     ]
    }
   ],
   "source": [
    "# 调用函数\n",
    "my_function(38)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "ab3fa77d-c2fc-441b-ac7f-7974d5ae96d6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "global_x: 8\n"
     ]
    }
   ],
   "source": [
    "# 在函数外部访问全局变量\n",
    "print(\"global_x:\", global_x) # global_x: 88"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "cdd73fd2-cc30-4618-854d-6318b34feb0b",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'local_x' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[5], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mlocal_x:\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[43mlocal_x\u001b[49m)\n",
      "\u001b[1;31mNameError\u001b[0m: name 'local_x' is not defined"
     ]
    }
   ],
   "source": [
    "print(\"local_x:\", local_x) \n",
    "# 报错，NameError: name 'local_x' is not defined"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
